<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport"
			content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.css" rel="stylesheet" />
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">我的位置</h1>
		</header>
		<div class="mui-content">
			<div id="map" style="height: 300px;"></div>
		</div>


		<script src="../js/mui.js"></script>
		<!--加载GPS组件-->
		<script src="../js/gps.js"></script>
		<!--加载高德地图组件-->
		<script src="../js/map.js"></script>
		<script src="../js/request.js"></script>
		<script src="../js/storage.js"></script>
		<script src="../js/date.js"></script>
		<script type="text/javascript">
			//定义全局变量
			let map = null;
			//获取当前登录用户
			let user = userCache.getUser();

			let gpsLocation = {
				userId: user.userId,
				deviceNo: userCache.getDeviceNo(),
				lng: null,
				lat: null,
				coordsType: '高德地图',
				dataTime: null,
			}
			let marker = null;

			mui.init();
			GDMapTool.init();

			mui.plusReady(function() {
				// 使用高德地图地位模块获取位置信息
				let gps = getGPS();
				gps.setOptions({
					getLocationCallback: (position) => {
						let lng = position.coords.longitude; //经度
						let lat = position.coords.latitude; //纬度
						console.log('获取位置信息回调', '坐标类型', position.coordsType, '纬度', lat, '经度', lng);
						//创建地图
						map = GDMapTool.createMap('map', lng, lat)

						//描点
						if (marker) {
							//删除旧的标记
							marker.setMap(null);
						}
						marker = map.marker(lng, lat);
						saveLocation(lng, lat);
					},
					watchLocationCallback: (position) => {
						let lng = position.coords.longitude; //经度
						let lat = position.coords.latitude; //纬度
						console.log('实时获取位置信息回调', '坐标类型', position.coordsType, '纬度', lat, '经度', lng);
						//描点
						if (marker) {
							//删除旧的标记
							marker.setMap(null);
						}
						map.marker(lng, lat);
						saveLocation(lng, lat);
					}
				})
				gps.getCurrentPosition();
			})

			//保存当前位置信息到后台
			function saveLocation(lng, lat) {
				if (!gpsLocation.deviceNo) {
					return
				}
				//获取今天日期
				let today = parseTime(new Date());
				gpsLocation.lng = lng;
				gpsLocation.lat = lat;
				gpsLocation.dataTime = today;

				request.post('/api/gps/location', gpsLocation, (data) => {
					//提交成功回调
					console.log('调用服务端更新地理定位成功');
				}, (error, thrown) => {
					//发生错误时，回调
					console.error('调用服务端更新地理定位失败', error);
				})
			}
		</script>
	</body>

</html>